package entry

import (
	"com.lh.mail.service/pb/mailbox"
	mail "com.lh.mail.service/service/mailbox"
	"com.lh.user.service/pb/user"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	"sync"
)

func InitGrpc(opts *user.ServeConfig, wg *sync.WaitGroup) {
	defer wg.Done()
	serve, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.Host, opts.Port))
	grpcServer := grpc.NewServer()
	if err != nil {
		log.Printf("failed to listen: %v", err)
	} else {
		mailbox.RegisterMailboxServiceServer(grpcServer, &mail.Mailbox{})
		log.Printf("gRPC server listening on: %s", serve.Addr())
	}
	if err = grpcServer.Serve(serve); err != nil {
		log.Printf("failed to serve: %v", err)
	}
}
